var init2 = 0;
var button;//按钮音乐对象
Page({
  data: {
    request_url: '',//url地址
    openid:"",//openid
    maskIndex:0,//面具序号
    image_photo:"",
    windowWidth: "",
    windowHeight: "",
  },
  onLoad: function (options) {
    var vm = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function (res) {
        vm.setData({
          windowWidth: res.windowWidth//可使用宽度
        })
        vm.setData({
          windowHeight: res.screenHeight//可使用高度
        })
        vm.setData({
          maskIndex: options.maskIndex,//面具
        })
      }
    });
    //获取url与openId
    //获取接口地址
    var request_url = wx.getStorageSync('request_url')
    var openid = wx.getStorageSync('openid')
    vm.setData({
      request_url: request_url,
      openid: openid
    })
  },
  choice: function () {
    this.button("button1",true,false)
    let vm = this
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        vm.setData({
          textHidden: true,
          image_photo: tempFilePaths,
          photoHidden: false
        })
        wx.uploadFile({
          url: vm.data.request_url + '/MianJuJiang/h5/putOnMask',
          filePath: tempFilePaths[0],
          name: 'photo',
          header:{
            "Content-Type": "multipart/form-data"
          },
          formData:{
            "openID": vm.data.openid,
            "maskIndex": vm.data.maskIndex
          },
          success: function (res) {
            console.log("------------------------------")
            console.log(res.data)
            console.log(tempFilePaths[0])
            wx.setStorage({
              key: 'html',
              data: res.data
            })
            wx.redirectTo({
              //跳转
              url: '../puton/puton',
            })
          }
        })        
      }
    })

  },
  button: function (u, a, l) {
    if (init2 == 0) {
      init2++;
      button = wx.createInnerAudioContext()
      button.autoplay = a;
      var urlKey = u;
      var savedFilePath = wx.getStorageSync(urlKey);
      button.src = savedFilePath;
      button.loop = l;
    } else {
      button.autoplay = a;
      button.src = wx.getStorageSync(u);
      button.loop = l;
    }
  },
  returnLevel: function () {
    this.button("button1", true, false);
    wx.redirectTo({
      //跳转
      url: '../level/level',
    })
  }
})